package com.cityguardian.cityguardianapp;

//import com.cityguardian.model.Alerta;
//import com.cityguardian.model.Usuario;
import com.cityguardian.threads.AsyncTaskGetUserAlert;
import com.cityguardian.utils.Utils;

import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class AlertList2Activity extends ActionBarActivity {
	
	private ScrollView scrollView;
	
	//public Usuario usuario;
	//public Alerta alerta;
	public ProgressDialog dialog;
	public String tipo;
	private String idPostServicio;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setupActionBar();
		
		scrollView = new ScrollView(this);
		LinearLayout linearLayout = new LinearLayout(this);		
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		Log.d("AlertList2", "Received: " + extras.toString());
		String texto = intent.getStringExtra("texto");
		idPostServicio = intent.getStringExtra("idPostServicio");
		tipo = intent.getStringExtra("tipo");
		final String idUsuario = intent.getStringExtra("user");
		final String idAlerta = intent.getStringExtra("idAlerta");
		Log.d("AlertList2", "Received: " +texto+idUsuario+idAlerta);
		//for (int i=0;i<3;i++){
			TextView textView = new TextView(this);
			textView.setText(texto);
			textView.setTextAppearance(this, android.R.style.TextAppearance_Medium);
			//Drawable d= new 
			textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.demo_user, 0, 0, 0);
			textView.setOnClickListener(new View.OnClickListener() {

			    @Override
			    public void onClick(View v) {
			        
			    	goDetailActivity(idUsuario,idAlerta);
			    	
			    }
			});
			linearLayout.addView(textView);
		//}
		
		scrollView.addView(linearLayout);
		this.setContentView(scrollView);
		
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
		else {
		    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.alert_list, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				onBackPressed();
				return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void sendUserForFillActivity() {
    	Intent intent = new Intent(this, FillAlertActivity.class);
    	startActivity(intent);
	}
	
	
	protected void goDetailActivity(String idUsuario, String idAlerta) {
		
        if (Utils.haveNetworkConnection(this)){
    		AsyncTaskGetUserAlert thread = new AsyncTaskGetUserAlert(this,idUsuario,idAlerta,tipo,idPostServicio);
    		thread.execute();	
		}
		else{
			Toast.makeText(getBaseContext(), 
					"Intente nuevamente. No Hay Conexion a Internet!", Toast.LENGTH_LONG).show();
		}
        	
	}
	
}
